package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Transactional(rollbackFor = Exception.class)
public class BoyAPI {
    @Autowired
    private BoyRepository boyRepository;
    @Autowired
    private BoyService boyService;


    /**
     * 查询男生列表
     *
     * @return
     */
    @GetMapping(value = "/boys")
    public List<Boy> boyList() {

        try {

        } catch (Exception e) {
        }

        return boyRepository.findAll();
    }
    /**
     * 新增一个男生
     * @param cupSize
     * @param age
     * @return
     */
	/*@PostMapping(value="/boys")
	public Boy boyAdd(@RequestParam("cupSize") String cupSize,
						 @RequestParam("age") Integer age) {
		Boy boy=new Boy();
		boy.setAge(age);
		boy.setCupSize(cupSize);
		boyRepository.save(boy);
		return boyRepository.save(boy);
	}*/

    /**
     * 对象的方式添加一个男生
     *
     * @param boy BindingResult  bindingResult错误对象
     * @return
     */
    @PostMapping(value = "/boys")
    public Result<Boy> boyAdd(@Valid Boy boy, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            //获取错误信息
            return ResultUtil.error("1", bindingResult.getFieldError().getDefaultMessage());
        }
        boy.setAge(boy.getAge());
        boy.setCupSize(boy.getCupSize());
        return ResultUtil.success(boyRepository.save(boy));
    }

    /**
     * 查询一个男生
     *
     * @PathVariable 路径中的变量
     */
    @GetMapping(value = "/boys/{id}")
    public Boy getOneBoy(@PathVariable("id") Integer id) {

        return boyRepository.findOne(id);
    }

    /**
     * 更新
     * put方式不能用formdate格式 要用x-www-form-urlencoded方式
     */
    @PutMapping(value = "/boys/{id}")
    public Boy updateBoy(@PathVariable("id") Integer id,
                         @RequestParam("age") Integer age,
                         @RequestParam("cupSize") String cupSize) {
        Boy boy = new Boy();
        boy.setId(id);
        boy.setAge(age);
        boy.setCupSize(cupSize);
        return boyRepository.save(boy);
    }

    /**
     * 删除
     * 用DELETE请求方式
     */
    @DeleteMapping(value = "/boys/{id}")
    public String deleteBoy(@PathVariable("id") Integer id) {
        boyRepository.delete(id);
        return "ok";
    }

    /**
     * 通过年龄查询男生列表
     */
    @GetMapping(value = "/boyss/{age}")
    public List<Boy> boyListByAge(@PathVariable("age") Integer age) {
        return boyRepository.findByAge(age);
    }

    /**
     * 同时添加两个男生
     */
    @PostMapping(value = "boys/two")
    public void Two() {
        boyService.insertTwo();
    }


    /**
     * 通过id判断男生年龄 属于小学还是初中
     *
     * @param id
     */
    @GetMapping(value = "boys/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id) throws Exception {
        boyService.getAge(id);
    }
    
}










